<%@ page language="java" errorPage="../error/exceptionHandler.jsp" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="org.cgs.web.database.DatabaseConnector" %>
<%
     Logger LOGGER = Logger.getLogger("instructor.jsp");
     String courseCode = "";
     String netID = "";

     if(request.getParameter("courseCode") != null)
     {
          courseCode = request.getParameter("courseCode");
     }	

     if(request.getParameter("netID") != null)
     {
          netID = request.getParameter("netID");
     }


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-AU">
  <head>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="../../css/sinorca-screen.css" media="screen" title="Sinorca (screen)" />
    <link rel="stylesheet alternative" type="text/css" href="../../css/sinorca-screen-alt.css" media="screen" title="Sinorca (alternative)" />
    <link rel="stylesheet" type="text/css" href="../../css/sinorca-print.css" media="print" />
    <title>Instructor Main Page</title>
  </head>

  <body>
    <div id="top"><a href="#main-copy" class="doNotDisplay doNotPrint">Skip to main content.</a></div>
    <div id="header">
      <div class="superHeader">
      </div>

      <div class="midHeader">
        <h1 class="headerTitle">Course Group Scheduler 3.0</h1>
      </div>
    </div>

    <!-- ##### Side Bar ##### -->
    <div id="side-bar">
      <div>
        <p class="sideBarTitle">Admin Menu</p>
        <ul>
	  <li><a href="addInstructor.jsp">Add Instructors</a></li>
	  <li><a href="viewInstructors.jsp">View Instructors</a></li>
        </ul>
      </div>
    </div>

    <!-- ##### Main Copy ##### -->

    <div id="main-copy">
      <h1 id="introduction">Welcome</h1>
      <p></p>

      <form name="f" method="post" action="/cgs/EditInstructor">
      <table>
	  <%
	     Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;

	     try
	     {
                con = DatabaseConnector.getInstance().getConnection();
		pstmt = con.prepareStatement("SELECT * FROM instructorAuthorization WHERE courseCode=? AND netID=?");
		pstmt.setString(1, courseCode);
		pstmt.setString(2, netID);
	        rs = pstmt.executeQuery();

	       while(rs.next())
	       {
		String password = rs.getString("password");
		String role = rs.getString("role");
 	%>

	<tr><td>Course Code:</td><td><INPUT type="text" name="courseCode" value="<%= courseCode %>" readonly></td></tr>
	<tr><td>Username:</td><td><INPUT type="text" name="username" value="<%= netID %>" readonly></td></tr>
	<tr><td>Password:</td><td><INPUT type="password" name="password" value="<%= password %>"></td></tr>

	<%
	if(role.equalsIgnoreCase("Admin"))
	{
	%>
	<tr><td>Role:</td><td><select name="role"><option value="Admin">Admin</option><option value="Instructor">Instructor</option></select></td></tr>
        <%
	}
	else
	{
	%>
	<tr><td>Role:</td><td><select name="role"><option value="Instructor">Instructor</option><option value="Admin">Admin</option></select></td></tr>
	<%
	}
	       } 
	    }
	    catch(Exception e)
	    {
                LOGGER.error("Exception thrown while atttempting to delete an instructor from DB", e);
	    }
	    finally
	    {
            try
            {
                // Close the ResultSet
                if (rs != null)
                {
                    rs.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up result set", e);
            }

            try
            {
                // Close the PreparedStatement
                if (pstmt != null)
                {
                    pstmt.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up prepared statement", e);
            }

            try
            {
                // Close the connection
                if (con != null)
                {
                    con.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up connection to the database", e);
            }
	    }
	%>
      </table>

       <INPUT type="Submit" Value="Edit">
      </form>

      <a class="topOfPage" href="#top" title="Go to the top of this page">^ TOP</a>
    
    <!-- ##### Footer ##### -->
    <div id="footer">
        E-mail:&nbsp;<a href="./index.html" title="Email webmaster">jeffrey.s.garrett@gmail.com</a>
    </div>
  </body>
</html>
